package cn.edu.njust.alg.poj;

import java.util.Scanner;


public class Main_1160 {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int v = scanner.nextInt();
		int p = scanner.nextInt();
		int[] pos = new int[v + 1];
		int i, j, k, l;
		int sum;
		for (i = 1; i <= v; i++) {
			pos[i] = scanner.nextInt();
		}
		int[][] w = new int[v + 1][v + 1];
		for (i = 1; i <= v; i++) {
			for (j = i + 1; j <= v; j++) {
				l = (i + j) / 2;
				sum = 0;
				for (k = i; k <= j; k++) {
					sum += Math.abs(pos[k] - pos[l]);
				}
				w[i][j] = sum;
			}
		}
		int[][] dp = new int[p + 1][v + 1];
		int min, d;
		for (j = 1; j <= v; j++) {
			dp[1][j] = w[1][j];
		}
		for (i = 2; i <= p; i++) {
			for (j = i + 1; j <= v; j++) {
				min = Integer.MAX_VALUE;
				for (k = i-1; k < j; k++) {
					d = dp[i - 1][k] + w[k+1][j];
					if (min > d) {
						min = d;
					}
				}
				dp[i][j] = min;
			}
		}
		// ArrayUtils.printArray(w);
		// ArrayUtils.printArray(dp);
		System.out.println(dp[p][v]);
		scanner.close();
	}
}
